<!--
  收文->修改公文要素->生成编号
-->
<template>
	<van-action-sheet v-model="showContent" :close-on-click-overlay="false" @closed="toClose" 
			title="生成编号" style="height: 50%; margin-bottom: 3%;">			
		<view class="search-add">
			<van-form @submit="onSubmit">		
				<van-cell-group v-if="genType == 'sw'">
					<van-cell is-link title="字头" v-model="swWord" @click="showZt = true" />
					<van-action-sheet ref="docType" v-model="showZt" :actions="zt" cancel-text="取消"
						close-on-click-action @select="ztSelect" />									
					
					<van-cell is-link title="选择年份" v-model="swYear" @click="showSwYear = true" />
					<van-action-sheet ref="swYear" v-model="showSwYear" :actions="yearList" cancel-text="取消"
						close-on-click-action @select="swYearSelect" />
						
					<van-field v-model="swNum" label="公文字号" placeholder="请输入" input-align="right" />
				</van-cell-group>
				<van-cell-group v-if="genType == 'lw'">
					<van-field label="字头" v-model="lwWord" input-align="right" />
					
					<van-cell is-link title="选择年份" v-model="lwYear" @click="showLwYear = true" />
					<van-action-sheet ref="lwYear" v-model="showLwYear" :actions="yearList" cancel-text="取消"
						close-on-click-action @select="lwYearSelect" />
					
					<van-field v-model="lwNum" label="公文字号" placeholder="请输入" input-align="right" />	
				</van-cell-group>
			</van-form>
			<view class="css">
				<van-row type="flex" justify="space-around">
					<van-col span="10">
						<van-button type="info" round plain hairline block @click="toClose">取消</van-button>
					</van-col>
					<van-col span="10">
						<van-button type="info" round block @click="onSubmit">确定</van-button>
					</van-col>
				</van-row>
			</view>
		</view>
	</van-action-sheet>
</template>

<script>
	export default {
		props: {
			showGenerate: {
				type: Boolean,
				default: false
			},
			tagType: {
				type: String
			},
			generateParamObject:{
				type: Object
			}
		},
		data() {
			return {
				showContent: false,
				genType: '',
				yearList: [{
						name: '2021'
					}, {
						name: '2020'
					}, {
						name: '2019'
					}, {
						name: '2018'
					},
				],
				
				swId: '', //收文id
				swWordId: '', //字号id
				lwWord: '', //字头
				swWord: '',
				lwYear: '', //
				swYear: '', //
				lwNum: '',
				swNum: '',
				showZt: false, //显示字头下拉列表
				showLwYear: false, //显示年份下拉列表
				showSwYear: false, //显示年份下拉列表
				
				zt:[],
				
			};
		},
		mounted() {
			//回显
			this.getGwWordList('sw')//获取发文函的字头列表
		},
		watch: {
			showGenerate(val) {
				this.showContent = val
			},
			tagType(val) {
				this.genType = val
			},
			generateParamObject: {
				handler(newObj, oldObj) {
					console.log('---------------watch:' + JSON.stringify(newObj));
					this.swId = newObj.swId
					this.swWordId = newObj.swWordId
					this.swWord = newObj.swWord
					this.swNum = newObj.swNum							
					if(newObj.swYear == null || newObj.swYear == ''){
						this.swYear = this.dict.getCurrentYear()
					} else {
						this.swYear = newObj.swYear
					}
					if(newObj.swWordId != null && newObj.swWordId != '') {
						this.getMaxfwNum()
					}	
					this.lwWord = newObj.fwWord
					this.lwNum = newObj.fwNum
					if(newObj.fwYear == null || newObj.fwYear == ''){
						this.lwYear = this.dict.getCurrentYear()
					} else {
						this.lwYear = newObj.fwYear
					}
				},
				immediate: true,
				deep: true
			}
		}, 
		created() {
			this.yearList=this.dict.getYearArr()
			console.log('-----------------created');
		},
		methods: {
			//字头选择
			ztSelect(item) {
				console.log("item=============: " + JSON.stringify(item));
				this.swWord = item.name
				this.swWordId = item.wordId
				this.getMaxfwNum()
				this.showZt = false				
			},
			
			//年份选择
			swYearSelect(item) {
				this.swYear = item.name
				this.getMaxfwNum()
				this.showSwYear = false
			},
			lwYearSelect(item) {
				this.lwYear = item.name
				this.getMaxfwNum()
				this.showLwYear = false
			},
			
			// 关闭
			toClose() {
				this.$emit('closeGenerate', this.showContent);
			},
			// 保存
			onSubmit() {
				let genData = {
					'type': this.genType,
					'lwWord': this.lwWord,
					'swWord': this.swWord,
					'swYear': this.swYear,
					'lwYear': this.lwYear,
					'swNum': this.swNum,
					'lwNum': this.lwNum,
					'swWordId': this.swWordId					
				};
				this.$emit('saveGenerate', genData);
			},
			
			
			/**
			 * 获取字头列表
			 * @param {Object} type   type=sw
			 */
			getGwWordList(type){
				var _this = this
				this.$httploading({
				  url: this.$http.adornUrl('/caac/oa/component/word/getwordbyuserandtype'),
				  method: 'get',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
					  'type': type
				  })
				}).then(({data}) => {
					if(data.code==200){
						if(data.data !==undefined && data.data.length > 0 ){
							_this.zt=[]  //置空
							data.data.forEach((item)=>{
								_this.zt.push({name:item.wordName,wordId:item.wordId})
							})
						}
					}
				})
			},

			/**
			 * 获取收文字号年后面的号数
			 */
			getMaxfwNum(){
				var _this = this
				this.$http({
				  url: this.$http.adornUrl('/caac/oa/component/word/getmaxfwnum'),
				  method: 'get',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
					  'fwWord':this.swWordId,
					  'fwYear':this.swYear,
					  'type':'sw',
					  'fwId':this.swId
				  })
				}).then(({data}) => {
					//{"code":200,"data":{"data":0,"message":"获取发文号成功!"},"message":null} 
					console.log("获取发文字号年后面的号数=======data: " + JSON.stringify(data));
					if(data.code==200){
						this.swNum = data.data.data
					}
				})
			},
		},
	};
</script>

<style scoped>
	.css {
		text-align: center;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 3%;
	}
	/deep/ .van-cell__title {
		min-width: 30%;
	}
	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}
</style>
